<?php
namespace App\Api\Controllers;
use Common\ApiController;
use Common\Common;
use Common\Models\AppConfig;

/**
 * @class 生成海报测试类
 */
class ImagesController extends ApiController{

    public function initialize()
    {
        parent::initialize();
    }



    //生成商品图片
    public function goods_posterAction(){
        if(!isset($this->params['title']) || empty($this->params['title'])){
            $this->fail(-1,'商品标题不能为空');
        }
        if(!isset($this->params['monthsale']) || empty($this->params['monthsale'])){
            $this->fail(-1,'购买人数不能为空');
        }
        if(!isset($this->params['endprice']) || empty($this->params['endprice'])){
            $this->fail(-1,'最后价格不能为空');
        }
        if(!isset($this->params['price']) || empty($this->params['price'])){
            $this->fail(-1,'价格不能为空');
        }
        if(!isset($this->params['img_url']) || empty($this->params['img_url'])){
            $this->fail(-1,'背景海报不能为空');
        }
        if(!isset($this->params['taoword']) || empty($this->params['taoword'])){
            $this->fail(-1,'淘口令不能为空');
        }
        if(!isset($this->params['couponmoney'])){
            $this->fail(-1,'优惠券不能为空');
        }
        $FAppConfigDb = new AppConfig();
        $appConfig = $FAppConfigDb->find_list("base_config");
        if(!isset($this->params['shoptype']) || empty($this->params['shoptype'])){
            $this->params['site_icon']=$appConfig['qiniu_url'].'tm.png';
        }else{
            switch($this->params['shoptype']){
                case '淘宝':
                    $this->params['site_icon']=$appConfig['qiniu_url'].'tb.png';
                    break;
                case '天猫':
                    $this->params['site_icon']=$appConfig['qiniu_url'].'tm.png';
                    break;
                case '拼多多':
                    $this->params['site_icon']=$appConfig['qiniu_url'].'pdd.png';
                    break;
                case '京东':
                    $this->params['site_icon']=$appConfig['qiniu_url'].'jd.png';
                    break;
                case '抖音':
                    $this->params['site_icon']=$appConfig['qiniu_url'].'jd.png';
                    break;
                default:
                    $this->params['site_icon']=$appConfig['qiniu_url'].'tm.png';
                    break;
            }
        }
        $posts = $this->posterplugs->share_goods_poster($this->params);
        if($posts['code']){
            $data['img_url']=$posts['data'];
            $this->ok($data);
        }else{
            $this->fail(-1,'海报图片生成失败');
        }
    }



    /**
     * 邀请海报
     */
    public function invite_posterAction(){
        if(!isset($this->params['img_url']) || empty($this->params['img_url'])){
            $this->fail(-1,'背景海报不能为空');
        }
        if(!isset($this->params['url']) || empty($this->params['url'])){
            //$this->fail(-1,'跳转地址不能为空');
            $this->params['url']='https://www.pgyer.com/COmi';
        }
        if(!isset($this->params['invite_code']) || empty($this->params['invite_code'])){
            $this->fail(-1,'邀请码不能为空');
        }
        $down_url=$this->params['url'];
        $posts = $this->posterplugs->invite_poster($this->params['img_url'],$down_url,$this->params['invite_code']);
        if($posts['code']){
            $data['img_url']=$posts['data'];
            $this->ok($data);
        }else{
            $this->fail(-1,'海报图片生成失败');
        }
    }

}
